<template>
  <div>
    <div style="margin: 10px 0;">

      <el-button type="primary" icon="el-icon-plus" @click="dialogVisible = true" >新增菜单</el-button>
      <el-input placeholder="请输入名查询" v-model="name" clearable style="width: 200px;margin-left: 5px"></el-input>
      <el-button type="primary" icon="el-icon-search" style="margin-left: 5px" @click="search">查询</el-button>
      <el-button type="danger" icon="el-icon-delete" @click="delBatch">批量删除</el-button>
    </div>
    <el-table :data="tableData" @selection-change="handleSelectionChange" style="width: 100%"
              :header-cell-style="{background:'#e7e8ef',color:'#4f5257'}"
               fixed border row-key="id"  default-expand-all>
      <el-table-column
          prop="id"
          type="selection"
          width="40">
      </el-table-column>
      <el-table-column prop="id" label="ID" width="100">
      </el-table-column>
      <el-table-column prop="name" label="菜单名称" ></el-table-column>
      <el-table-column prop="path" label="菜单路径" ></el-table-column>
      <el-table-column prop="pagePath" label="菜单路径" ></el-table-column>
      <el-table-column prop="sort" label="菜单排序(按大小)" ></el-table-column>
      <el-table-column label="菜单图标" >
        <template slot-scope="scope">
          <i :class="scope.row.icon" style="margin-right: 5px"/>{{scope.row.icon}}
        </template>
      </el-table-column>
      <el-table-column prop="description" label="菜单描述" ></el-table-column>
      <el-table-column
          label="操作"
          width="280">
        <template slot-scope="scope">
          <el-button type="info" class="el-icon-edit" size="mini" @click="handleAdd(scope.row.id)" v-if="!scope.row.pid && !scope.row.path">新增子菜单</el-button>
          <el-button type="success" class="el-icon-edit" size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-popconfirm
              style="margin-left: 5px"
              confirm-button-text='确定'
              cancel-button-text='取消'
              icon="el-icon-info"
              icon-color="red"
              title="您确定删除吗？"
              @confirm="handleDelete(scope.$index, scope.row)"
          >
            <el-button type="danger" size="mini" class="el-icon-remove-outline" slot="reference">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>

    </el-table>

    <el-dialog
        title="菜单分配"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="handleClose">

      <el-form ref="form" :model="form" label-width="120px">
        <el-form-item label="菜单名">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="菜单路径">
          <el-input v-model="form.path"></el-input>
        </el-form-item>
        <el-form-item label="页面路径">
          <el-input v-model="form.pagePath"></el-input>
        </el-form-item>
        <el-form-item label="排序大小">
          <el-input v-model="form.sort"></el-input>
        </el-form-item>
        <el-form-item label="菜单图标">
          <template slot-scope="scope">
            <el-select clearable v-model="form.icon" placeholder="请选择" style="width: 100%;">
              <el-option v-for="i in options"  :label="i.name" :value="i.name">
                <i :class="i.value" style="margin-right: 5px"/>{{i.name}}
              </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item label="菜单描述">
          <el-input v-model="form.description"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
             <el-button type="primary" @click="save">确 定</el-button>
            </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Menu",
  data(){
    return{
      name: '',
      tableData:[],
      currentPage: 4,
      dialogVisible: false,
      form: {},
      ids:[],
      multipleSelection: [],
      options: []
    }
  },
  created() {
    this.load()
  },
  methods:{
    reset(){
      this.address="";
      this.phone="";
      this.nickName="";
      this.load()
      this.$message({
        message: '重置成功',
        type: 'success'
      });
    },
    search(){
      if (this.address!='' || this.nickName!='' || this.phone!=''){
        this.load()
        this.$message({
          message: '查询成功',
          type: 'success'
        });
      }else{
        this.$message({
          message: '请输入查询内容',
          type: 'warning'
        });
      }
    },
    load(){
      this.request.get("/menu",{
            params:{
              name: this.name
            }
          }
      ).then(res =>{
        this.tableData=res.data
      })
    },
    save(){
      this.request.post("/menu",this.form).then(res => {
        if (res.code === 200 ){
          this.$message.success("保存成功")
          this.dialogVisible=false
          this.form={}
          this.load()
        }else {
          this.$message.error(res.message)
        }
      })
    },
    handleAdd(pid) {
      this.dialogVisible =true
      this.form = {}
      if (pid){
        this.form.pid = pid
      }
      //获取图标数据
      this.request.get("/menu/icons").then(res =>{
        this.options = res.data
      })
    },
    handleEdit(row) {
      this.form=row
      this.dialogVisible=true

      //获取图标数据
      this.request.get("/menu/icons").then(res =>{
        this.options = res.data
      })
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
    },
    handleDelete(index, row) {
      this.request.delete("/menu/"+(index,row).id).then(res=>{
        if (res.code === 200 ){
          this.load()
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }
      })
    },
    delBatch(){
      let ids = this.multipleSelection.map(v =>v.id)
      if (ids.length!=0){
        this.$confirm('你正在进行批量删除操作, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.request.post("/menu/batch/", ids).then(res => {
            if (res.code === 200 ){
              this.load()
              this.$message({
                type: 'success',
                message: '批量删除成功!'
              });
            }else {
              this.$message({
                type: 'error',
                message: '删除失败!'
              });
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      }else {
        this.$message.warning("请选择删除记录")
      }
    },
    /**
     * 选择框
     * */
    handleSelectionChange(val) {
      this.multipleSelection = val;
      for (let i=1; i<=val; i++)
        console.log(this.multipleSelection[i])
    },
    /**
     * 自定义操作列
     * @param index
     * @param row
     */


    handleSizeChange(val) {
      this.pageSize = val;
      this.load()
    },
    handleCurrentChange(val) {
      this.pageNum=val
      this.load()
    }
  }
}
</script>

<style scoped>

</style>